//
//  XWTableViewCell_scoll.m
//  网易新闻
//
//  Created by dllo on 15/9/21.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "XWTableViewCell_scoll.h"
#import "SDWebImage/UIImageView+WebCache.h"
#import "Settings.h"
@interface XWTableViewCell_scoll()<UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, retain)UILabel *lab;
@property(nonatomic, retain)NSMutableArray *labArr;
@property(nonatomic, retain)UIPageControl *page;
@property(nonatomic, retain)NSTimer *timer;
@end
@implementation XWTableViewCell_scoll
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        
        self.scroll = [[UIScrollView alloc]init];
        self.scroll.backgroundColor = [UIColor grayColor];
        self.scroll.pagingEnabled = YES;
        self.scroll.showsHorizontalScrollIndicator = NO;
        self.scroll.showsVerticalScrollIndicator= NO;
        self.scroll.delegate = self;
        self.scroll.bounces = NO;
        [self.contentView addSubview:self.scroll];
        [self.scroll release];
        
        self.page = [[UIPageControl alloc]init];
        self.page.numberOfPages = 4;
        self.page.currentPageIndicatorTintColor = [[Settings shareSettings]color:BlackToWhite];
        self.page.pageIndicatorTintColor = [UIColor grayColor];
        [self.page addObserver:self forKeyPath:@"currentPage" options:NSKeyValueObservingOptionNew context:@"currentPage"];
        [self.contentView addSubview:_page];
        
        self.lab = [[UILabel alloc]init];
        self.lab.textColor = [[Settings shareSettings]color:BlackToWhite];
        self.lab.textAlignment = NSTextAlignmentLeft;
        self.lab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_lab];
        [_lab release];
        
        self.labArr = [NSMutableArray array];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changePage) userInfo:nil repeats:YES];
        [self notificationHandle];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.scroll.frame =CGRectMake(0, 0, 375, 215);
    self.scroll.contentSize = CGSizeMake(2250, 215);
    self.page.frame = CGRectMake(290, 225, 75, 15);
    self.lab.frame = CGRectMake(30, 225, 260, 15);
    self.page.currentPage = 0;
    self.scroll.contentOffset = CGPointMake((self.page.currentPage+1)*375, 0);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x<375) {
        [_scroll setContentOffset: CGPointMake(1500, 0) animated:NO];
    }else if (scrollView.contentOffset.x>1500){
        [_scroll setContentOffset: CGPointMake(375, 0) animated:NO];
    }
    _page.currentPage = scrollView.contentOffset.x/375-1;
}

- (void)changePage{
    if (self.page.currentPage == 3) {
        [self.scroll setContentOffset: CGPointMake(1875, 0) animated:YES];
        self.page.currentPage =0;
    }else{
        if (self.page.currentPage == 0) {
            [self.scroll setContentOffset: CGPointMake(375, 0) animated:NO];
        }
        self.page.currentPage ++;
        [self.scroll setContentOffset: CGPointMake((self.page.currentPage+1)*375, 0) animated:YES];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    self.lab.text = self.labArr[self.page.currentPage];
}

- (void)setMod:(XinwenModel *)mod{
    if (_mod != mod) {
        [_mod release];
        _mod = [mod retain];
    }
    self.height = 245;
    
    UIImageView *firstView = [[UIImageView alloc]init];
    XinwenModel *adsMod = [XinwenModel modWithDic:mod.ads[2]];
    firstView.frame = CGRectMake(0, 0, 375, 215);
    [firstView sd_setImageWithURL:adsMod.imgsrc placeholderImage:nil];
    [_scroll addSubview:firstView];
    [firstView release];
    
    UIImageView *secondView = [[UIImageView alloc]init];
    secondView.frame = CGRectMake(375, 0, 375, 215);
    [secondView sd_setImageWithURL:mod.imgsrc placeholderImage:nil];
    [_scroll addSubview:secondView];
    [secondView release];
    [self.labArr addObject:mod.title];
    self.lab.text = self.labArr[0];
    
    for (int i=0; i<3; i++) {
        UIImageView *tempImgV = [[UIImageView alloc]init];
        XinwenModel *adsMod = [XinwenModel modWithDic:mod.ads[i]];
        [self.labArr addObject:adsMod.title];
        tempImgV.frame = CGRectMake(750+i*375, 0, 375, 215);
        [tempImgV sd_setImageWithURL:adsMod.imgsrc placeholderImage:nil];
        [_scroll addSubview:tempImgV];
        [tempImgV release];
    }
    
    UIImageView *lastView = [[UIImageView alloc]init];
    lastView.frame = CGRectMake(1875, 0, 375, 215);
    [lastView sd_setImageWithURL:mod.imgsrc placeholderImage:nil];
    [_scroll addSubview:lastView];
    [lastView release];

}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.page.currentPageIndicatorTintColor = [[Settings shareSettings]color:BlackToWhite];
        self.lab.textColor = [[Settings shareSettings]color:BlackToWhite];
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    }];
}

- (void)dealloc{
    _scroll.delegate = nil;
    [_page removeObserver:self forKeyPath:@"currentPage"];
    [_page release];
    [_lab release];
    [_labArr release];
    [_scroll release];
    [self.timer invalidate];
    self.timer = nil;
    [super dealloc];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
